home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / database / cre.c < prev    next >
Text File  |  1984-05-21  |  4KB  |  149 lines

  1. /* SDB - relation creation routines */
  2.  
  3. #include "sdbio.h"
  4.  
  5. /* db_rcreate(rname) - begin the creation of a new relation */
  6. struct relation *db_rcreate(rname)
  7.   char *rname;
  8. {
  9.     struct relation *rptr;
  10.  
  11.     /* allocate the relation structure */
  12.     if ((rptr = calloc(1,sizeof(struct relation))) == NULL)
  13.         return (db_nerror(INSMEM));
  14.  
  15.     /* initialize the relation structure */
  16.     stccpy(rptr->rl_name,rname,RNSIZE);
  17.     rptr->rl_tcnt = 0;
  18.     rptr->rl_tmax = 0;
  19.     rptr->rl_data = 512;
  20.     rptr->rl_size = 1;
  21.     rptr->rl_header.hd_attrs[0].at_name[0] = 0;
  22.  
  23.     /* return the new relation structure pointer */
  24.     return (rptr);
  25. }
  26.  
  27. /* db_rcheader - create the relation header */
  28. int db_rcheader(rptr)
  29.   struct relation *rptr;
  30. {
  31.     char rname[RNSIZE+1],filename[RNSIZE+13];
  32.  
  33.     /* initialize the relation file header */
  34.     db_cvbytes(rptr->rl_tcnt,rptr->rl_header.hd_tcnt);
  35.     db_cvbytes(rptr->rl_tmax,rptr->rl_header.hd_tmax);
  36.     db_cvbytes(rptr->rl_data,rptr->rl_header.hd_data);
  37.     db_cvbytes(rptr->rl_size,rptr->rl_header.hd_size);
  38.  
  39.     /* create the relation file name */
  40.     stccpy(rname,rptr->rl_name,RNSIZE); rname[RNSIZE] = 0;
  41.     sprintf(filename,"%s.sdb",rname);
  42.  
  43.     /* create the relation file */
  44.     if ((rptr->rl_fd = creat(filename,0)) == -1) {
  45.         free(rptr);
  46.         return (db_ferror(RELCRE));
  47.     }
  48.  
  49.     /* write the header to the relation file */
  50.     if (write(rptr->rl_fd,&rptr->rl_header,512) != 512) {
  51.         close(rptr->rl_fd);
  52.         free(rptr);
  53.         return (db_ferror(BADHDR));
  54.     }
  55.  
  56.     /* return successfully */
  57.     return (TRUE);
  58. }
  59.  
  60. /* db_rctuples - create the relation tuples */
  61. int db_rctuples(rptr,tcnt)
  62.   struct relation *rptr; unsigned int tcnt;
  63. {
  64.     unsigned int i;
  65.     char *tbuf;
  66.  
  67.     /* store the number of tuples */
  68.     rptr->rl_tmax = tcnt;
  69.  
  70.     /* allocate a tuple buffer */
  71.     if ((tbuf = calloc(1,rptr->rl_size)) == NULL)
  72.         return (db_ferror(INSMEM));
  73.  
  74.     /* write null tuples into the file */
  75.     for (i = 0; i < tcnt; i++)
  76.         if (write(rptr->rl_fd,tbuf,rptr->rl_size) != rptr->rl_size) {
  77.             free(tbuf);
  78.             return (db_ferror(INSBLK));
  79.         }
  80.  
  81.     /* free the tuple buffer */
  82.     free(tbuf);
  83.  
  84.     /* return successfully */
  85.     return (TRUE);
  86. }
  87.  
  88. /* db_rcdone(rptr) - finish the creation of a new relation */
  89. int db_rcdone(rptr)
  90.   struct relation *rptr;
  91. {
  92.     /* initialize the relation file header */
  93.     db_cvbytes(rptr->rl_tcnt,rptr->rl_header.hd_tcnt);
  94.     db_cvbytes(rptr->rl_tmax,rptr->rl_header.hd_tmax);
  95.  
  96.     /* write the header to the relation file */
  97.     lseek(rptr->rl_fd,0L,0);
  98.     if (write(rptr->rl_fd,&rptr->rl_header,512) != 512) {
  99.         close(rptr->rl_fd);
  100.         free(rptr);
  101.         return (db_ferror(BADHDR));
  102.     }
  103.  
  104.    /* close the relation file */
  105.     close(rptr->rl_fd);
  106.  
  107.     /* free the relation structure */
  108.     free(rptr);
  109.  
  110.     /* return successfully */
  111.     return (TRUE);
  112. }
  113.  
  114. /* db_rcattr(rptr,aname,type,size) - add an attribute to relation being created
  115. */
  116. int db_rcattr(rptr,aname,type,size)
  117.   struct relation *rptr; char *aname; int type,size;
  118. {
  119.     int i;
  120.  
  121.     /* look for attribute name */
  122.     for (i = 0; i < NATTRS; i++)
  123.         if (rptr->rl_header.hd_attrs[i].at_name[0] == 0)
  124.             break;
  125.         else if (db_sncmp(aname,rptr->rl_header.hd_attrs[i].at_name,ANSIZE) == 0
  126. )
  127.             return (db_ferror(DUPATT));
  128.  
  129.     /* check for too many attributes */
  130.     if (i == NATTRS)
  131.         return (db_ferror(MAXATT));
  132.  
  133.     /* store the new attribute */
  134.     stccpy(rptr->rl_header.hd_attrs[i].at_name,aname,ANSIZE);
  135.     rptr->rl_header.hd_attrs[i].at_type = type;
  136.     rptr->rl_header.hd_attrs[i].at_size = size;
  137.  
  138.     /* terminate the attribute table */
  139.     if (++i != NATTRS)
  140.         rptr->rl_header.hd_attrs[i].at_name[0] = 0;
  141.  
  142.     /* update the tuple size */
  143.     rptr->rl_size += size;
  144.  
  145.     /* return successfully */
  146.     return (TRUE);
  147. }
  148.  
  149.